//一：将下面异步代码使用Promise方式改进
//例子是三个嵌套在一起的setTimeout的回调
//作业如下
//定义一个延时10ms的promise对象



function p(value){
    return new Promise((resolved,rejected) => {
        setTimeout(() => {
            resolved(value)
        },10)
    })
    
}
p().then(value =>{return p("hello")})
    .then(value => {return p(value+"lagou")})
    .then(value => {return p (value+"I love U")})
    .then(value => {
        console.log('---------------答案一-------------')
        console.log(value)
    })

//二：基于以下代码完成以下四个练习
const cars = [
    {name:'Ferrair FF',horsepower:660,dollar_value:700000,in_stock:true},
    {name:'Spyker C12 Zagato',horsepower:650,dollar_value:648000,in_stock:false},
    {name:'Jafuar XKR-S',horsepower:550,dollar_value:132000,in_stock:false},
    {name:'Audi R8',horsepower:525,dollar_value:114200,in_stock:false},
    {name:'Aston Martin One-77',horsepower:750,dollar_value:1850000,in_stock:true},
    {name:'Pagani Huayra',horsepower:700,dollar_value:1300000,in_stock:false}
]


//练习1：使用函数组合fp.flowRight()重新实现下面这个函数

console.log('---------------答案二练习1-------------')

const fp=require('lodash/fp')
let isLaskInStock=fp.flowRight(fp.prop('in_stock'),fp.last)
console.log(isLaskInStock(cars))


//练习2：使用fp.flowRight(),fp.prop()和fp.first()获取第一个car的name

console.log('---------------答案二练习2-------------')


let fr =fp.flowRight(fp.prop('name'),fp.first)
console.log(fr(cars))


//练习3：使用帮助函数_average重构averageDollarValue，使用函数组合方式实现

console.log('---------------答案二练习3-------------')


let  _average = function (xs){
    return fp.reduce(fp.add,0,xs) / xs.length
}
//
let averageDollarValue = function(cars){
   let arr = fp.flowRight(_average,fp.map(car=>car.dollar_value))
   return arr(cars)
}
//
console.log(averageDollarValue(cars))


//练习4：使用flowRight写一个sanitizeNames()函数，返回一个下划线连接的小写字符串，
//把数组中的name转换为这种形式，例如：sanitizeNames(['Hello World']) => ['Hello_world']

console.log('---------------答案二练习4-------------')


let _underscore = fp.replace(/\W+/g,"_")

function sanitizeNames(){
    return fp.flowRight(fp.map(name=>_underscore(name)))
 }

console.log(sanitizeNames()(['Hello World'])) 


//三：基于下面提供的代码，完成后续的四个练习
//support.js
class Container{
    static of(value){
        return new Container(value)
    }
    constructor(value){
        this._value = value
    }
    map(fn){
        return Container.of(fn(this._value))
    }
}

class MayBe{
    static of(x){
        return new MayBe(x)
    }
    isNothing(){
        return this._value === null || this._value === undefined
    }
    constructor(x){
        this._value = x
    }
    map(fn){
        return this.isNothing()?this:MayBe.of(fn(this._value))
    }
}
module.exports = {MayBe,Container}

//练习1：使用fp.add(x,y)和fp.map(f,x)创建一个能让functor里的值增加的函数ex1



console.log('---------------答案三练习1-------------')
//const fp = require('lodash/fp')
//const { MayBe,Container } = require('./support')

let maybe = MayBe.of([5,6,1])
let ex1 = (num) =>{
    let fn = fp.flowRight(fp.map(fp.add(num)))
    return maybe.map(fn)  
}
console.log(ex1(5))


//练习2：实现一个函数ex2，能够使用fp.first获取列表的第一个元素


console.log('---------------答案三练习2-------------')
//const fp = require('lodash/fp')
//const { MayBe,Container } = require('./support')
let xs = Container.of(['do','ray','me','fa','so','la','ti','do'])

let ex2 = () => {
    //实现的函数
    return xs.map(fp.first)._value
}
console.log(ex2()) 


//练习3：实现一个函数ex3，使用safeProp和fp.first找到user的名字的首字母

console.log('---------------答案三练习3-------------')


//const fp = require('lodash/fp')
//const { MayBe,Container } = require('./support')
let safeProp = fp.curry(function(x,o){
    return MayBe.of(o[x])
})
let user = {id:2,name:'Albert'}
let ex3 = () => {
    //实现的代码
    return safeProp('name',user).map(fp.first)._value
}
console.log(ex3())  



//练习4：使用Maybe重写ex4，不要有if语句


console.log('---------------答案三练习4-------------')
//const fp = require('lodash/fp')
//const { MayBe,Container } = require('./support')
// let  ex4 = function(n){
//     if(n){
//         return parseInt (n)
//     }
// }
let  ex4 = function(n){
    let m1 = new MayBe(n)
    let m2 = m1.map(parseInt)
    return m2._value
}
console.log(ex4(5))






